package com.aid.winter.controller;

import com.aid.winter.common.annotation.RequiredRole;
import com.aid.winter.common.constant.RoleConstants;
import com.aid.winter.common.response.Result;
import com.aid.winter.service.UserWhitelistService;
import com.aid.winter.dto.response.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import java.util.List;

@RestController
@RequestMapping("/user/whitelist")
@RequiredArgsConstructor
public class UserWhitelistController {

    private final UserWhitelistService whitelistService;

    /**
     * 检查当前用户是否在白名单中
     */
    @GetMapping("/check")
    @SaCheckLogin  // 添加登录校验注解
    public Result<Boolean> checkWhitelist() {
        Long userId = StpUtil.getLoginIdAsLong();
        return Result.ok(whitelistService.isUserInWhitelist(userId));
    }

    /**
     * 获取白名单用户列表
     */
    @GetMapping("/list")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<List<UserVO>> getWhitelistUsers() {
        return Result.ok(whitelistService.getWhitelistUsers());
    }

    /**
     * 添加用户到白名单
     */
    @PostMapping("/add")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<Void> addToWhitelist(@RequestParam String userId) {
        Long adminId = StpUtil.getLoginIdAsLong();
        whitelistService.addToWhitelist(Long.valueOf(userId), adminId);
        return Result.ok();
    }

    /**
     * 从白名单移除用户
     */
    @DeleteMapping("/remove")
    @RequiredRole(RoleConstants.ROLE_ADMIN)
    public Result<Void> removeFromWhitelist(@RequestParam String userId) {
        whitelistService.removeFromWhitelist(Long.valueOf(userId));
        return Result.ok();
    }
}